package com.reggie.console.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.reggie.acl.dataobject.Orders;
import com.reggie.common.util.ReggieContext;
import com.reggie.console.annotation.AuthRule;
import com.reggie.console.dto.ResultDTO;
import com.reggie.console.enums.ReggieRole;
import com.reggie.console.util.ResultBuilder;
import com.reggie.service.model.bo.OrdersBO;
import com.reggie.service.model.request.OrdersPageRequest;
import com.reggie.service.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author yth
 * @since 2023-02-04
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 根据订单号、下单时间范围，员工查询订单信息
     *
     * @param ordersPageRequest 条件
     * @return 数据
     */
    @GetMapping("/page")
    @AuthRule(roles = {ReggieRole.EMPLOYEE})
    public ResultDTO<IPage<OrdersBO>> page(OrdersPageRequest ordersPageRequest) {
        IPage<OrdersBO> pageInfo = ordersService.page(ordersPageRequest, false);
        return ResultBuilder.ok(pageInfo);
    }

    /**
     * 根据订单id，修改订单信息
     *
     * @param orders 订单
     * @return 信息
     */
    @PutMapping
    @AuthRule(roles = {ReggieRole.EMPLOYEE})
    public ResultDTO<String> update(@RequestBody Orders orders) {
        ordersService.updateById(orders);
        return ResultBuilder.ok("修改订单成功");
    }

    /**
     * 用户查询自己的历史订单详情
     *
     * @param ordersPageRequest 条件
     * @return 数据
     */
    @GetMapping("/userPage")
    @AuthRule(roles = {ReggieRole.CUSTOMER})
    public ResultDTO<IPage<OrdersBO>> userPage(OrdersPageRequest ordersPageRequest) {
        long userId = ReggieContext.getInstance().getUserId();
        ordersPageRequest.setUserId(userId);
        IPage<OrdersBO> pageInfo = ordersService.page(ordersPageRequest, true);
        return ResultBuilder.ok(pageInfo);
    }

    /**
     * 提交订单
     *
     * @param orders 订单
     * @return 消息
     */
    @PostMapping("/submit")
    @AuthRule(roles = {ReggieRole.CUSTOMER})
    public ResultDTO<String> submit(@RequestBody Orders orders) {
        // addressBookId, payMethod, remark
        long userId = ReggieContext.getInstance().getUserId();
        orders.setUserId(userId);
        ordersService.submit(orders);
        return ResultBuilder.ok("添加订单成功");
    }

    /**
     * 再来一单
     *
     * @param orders 订单（包含订单ia）
     * @return 消息
     */
    @PostMapping("/again")
    @AuthRule(roles = {ReggieRole.CUSTOMER})
    public ResultDTO<String> again(@RequestBody Orders orders) {
        long userId = ReggieContext.getInstance().getUserId();
        ordersService.again(orders.getId(), userId);
        return ResultBuilder.ok("再来一单成功");
    }

}

